package jcu.ict.profaid;

import java.util.ArrayList;
import java.util.List;

import com.sun.org.apache.xpath.internal.functions.Function;

import sun.org.mozilla.javascript.internal.regexp.SubString;

import android.annotation.SuppressLint;
import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

@SuppressLint("DefaultLocale")
public class MyAdapter extends ArrayAdapter<Object> {
		 
	public MyAdapter(Context form_my_subject, int resource,
			int textViewResourceId, List<Object> objects) {
		super(form_my_subject, resource, textViewResourceId, objects);
	}

	@SuppressLint("DefaultLocale")
	@Override
	public View getView(int position, View cv, ViewGroup parent) {
		ViewHolder vh;
		if (cv == null) {
			cv = super.getView(position, cv, parent);
			vh = new ViewHolder();
			vh.kiri = (TextView) cv.findViewById(R.id.txtUp);
			vh.kanan = (TextView) cv.findViewById(R.id.txtDown);

			cv.setTag(vh);
		} else {
			vh = (ViewHolder) cv.getTag();
		}
		Object objek = getItem(position);
		@SuppressWarnings("unchecked")
		ArrayList<Object> row = (ArrayList<Object>) objek;

		int i1 = row.get(0).toString().length();
		int iF1 = countLength(i1);
		
		int i2 = row.get(1).toString().length();
		int iF2 = countLength(i2);
		
		int longs = countAlphabets(iF1,iF2);
		
		int i3 = row.get(3).toString().length();
		int iF3 = countLength(i3);
		
		vh.kiri.setText( row.get(0).toString().toUpperCase().substring(0, iF1) + " - " + row.get(1).toString().toUpperCase().substring(0, longs));
		vh.kanan.setText(row.get(2).toString().toUpperCase() + " - " + row.get(3).toString().toUpperCase().substring(0, iF3));
		return cv;
	}

	public int countLength(int lengthLong){
		int iFinal1 = 0;
		if (lengthLong < 13){
			iFinal1 = lengthLong;
		}
		if (lengthLong >= 13){
			iFinal1 = 13;
		}
		return iFinal1;
	}
	
	public int countAlphabets(int f1, int f2){
		int longAlpha = 0;
		
		if(f1 < 13 && f2 >= 13){
			longAlpha = 26 - f1;
		}
		if(f1 < 13 && f2 < 13){
			longAlpha = f2;
		}
		if (f1 >= 13 && f2 < 13) {
			longAlpha = f2;
		}
		return longAlpha;
	}
	
	class ViewHolder {
		ImageView img;
		TextView kiri;
		TextView kanan;
	}


}
